package com.lmabbe.tenant.service.impls;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.mapper.SysParamsMapper;
import com.lmabbe.tenant.objects.converter.SysParamsConverter;
import com.lmabbe.tenant.objects.entity.SysParams;
import com.lmabbe.tenant.objects.entity.TenantInstallContext;
import com.lmabbe.tenant.objects.vo.SysParamsVO;
import com.lmabbe.tenant.service.SysParamsService;
import com.lmabbe.tenant.service.TenantInstallContextService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统参数
 *
 * @author lmabbe
 */
@Service
@AllArgsConstructor
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams>
        implements SysParamsService {

    private final TenantInstallContextService tenantInstallContextService;
    private final SysParamsConverter sysParamsConverter;

    /**
     * 获取参数构建列表
     *
     * @param tenantId 租户ID
     * @return {@link R}
     * @date 2022/3/27 11:30 AM
     * @author liming
     */
    @Override
    public List<SysParamsVO> getBuildList(String tenantId) {
        TenantInstallContext context = tenantInstallContextService.getById(tenantId);
        if (context != null) {
            return sysParamsConverter.paramBuild2ParamVo(context.getSysParams());
        } else {
            List<SysParams> sysParams = baseMapper.selectList(baseMapper.createQueryWrapper()
                    .eq(SysParams::getPlatform, false));
            return sysParamsConverter.list2Vos(sysParams);
        }
    }
}
